package com.xjscrm.console.service.customer.circle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.mobile.collect.api.dto.msg.MicaPublishSnsResultMsg;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.console.dto.circle.CircleTaskPublishDTO;
import com.xjscrm.console.entity.CircleTaskPublish;
import com.xjscrm.console.mapper.CircleTaskPublishMapper;
import com.xjscrm.console.mapper.WwDeviceMapper;
import com.xjscrm.console.vo.CircleTaskPublishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/4/9 上午10:26
 */
@Service
public class CircleTaskPublishService {

    @Resource
    private CircleTaskPublishMapper circleTaskPublishMapper;

    @Resource
    private WwDeviceMapper wwDeviceMapper;

    @Autowired
    @Lazy
    private CircleTaskService circleTaskService;


    public int deleteByPrimaryKey(Long id) {
        return circleTaskPublishMapper.deleteByPrimaryKey(id);
    }


    public int insertSelective(CircleTaskPublish record) {
        return circleTaskPublishMapper.insertSelective(record);
    }


    public CircleTaskPublish selectByPrimaryKey(Long id) {
        return circleTaskPublishMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(CircleTaskPublish record) {
        return circleTaskPublishMapper.updateByPrimaryKeySelective(record);
    }

    public void updatePublishStatus(@MerId Long merId, MicaPublishSnsResultMsg publishSnsResultMsg) {

        Long id = publishSnsResultMsg.getId();

        CircleTaskPublish circleTaskPublish = circleTaskPublishMapper.selectByPrimaryKey(id);
        if (Objects.isNull(circleTaskPublish)) {
            return;
        }
        CircleTaskPublish update = new CircleTaskPublish();
        update.setId(id);
        update.setStatus(publishSnsResultMsg.getSuccess() ? 31 : 32);
        update.setPostId(publishSnsResultMsg.getPostId());
        update.setCircleId(publishSnsResultMsg.getSnsId());
        if (!publishSnsResultMsg.getSuccess()) {
            update.setRemark(publishSnsResultMsg.getErrorMsg());
        }
        update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        circleTaskPublishMapper.updateByPrimaryKeySelective(update);

        //
        circleTaskService.triggerCompleteTaskIfAbsent(merId, circleTaskPublish.getTaskId());
    }

    public Object search(@MerId Long merId, Long staffId, CircleTaskPublishDTO source) {

        Page<CircleTaskPublish> page = new Page<>(source.getCurrent(), source.getSize());
        source.setStaffId(staffId);
        List<CircleTaskPublish> circleTaskPublishList = circleTaskPublishMapper.selectPage(page, source);

        List<CircleTaskPublishVO> resultRecord = new ArrayList<>();
        for (CircleTaskPublish circleTaskPublish : circleTaskPublishList) {
            CircleTaskPublishVO circleTaskPublishVO = new CircleTaskPublishVO();
            BeanUtils.copyProperties(circleTaskPublish, circleTaskPublishVO);

            WwDevice device = wwDeviceMapper.getById(circleTaskPublish.getDeviceUniqueId(), merId);
            if (Objects.nonNull(device)) {
                circleTaskPublishVO.setHeadImgUrl(device.getHeadImgUrl());
                circleTaskPublishVO.setDeviceName(device.getDeviceName());
            }
            resultRecord.add(circleTaskPublishVO);
        }

        return new Page<CircleTaskPublishVO>(page.getCurrent(), page.getSize(), page.getTotal()).setRecords(resultRecord);
    }

    public void updatePublishFailWithOffline(@MerId Long merId, Long id) {

        circleTaskPublishMapper.updateStatusFail(merId, id, "设备下线, 被忽略");

    }
}
